package com.nanhai.buddhist.college.module.collegebase.service.classroomcategory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classroomcategory.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classroomcategory.ClassroomCategoryDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.collegebase.convert.classroomcategory.ClassroomCategoryConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.classroomcategory.ClassroomCategoryMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.*;

/**
 * 教室分类 Service 实现类
 *
 * @author 南海佛学院
 */
@Service
@Validated
public class ClassroomCategoryServiceImpl implements ClassroomCategoryService {

    @Resource
    private ClassroomCategoryMapper classroomCategoryMapper;

    @Override
    public String createClassroomCategory(ClassroomCategoryCreateReqVO createReqVO) {
        // 插入
        ClassroomCategoryDO classroomCategory = ClassroomCategoryConvert.INSTANCE.convert(createReqVO);
        classroomCategoryMapper.insert(classroomCategory);
        // 返回
        return classroomCategory.getId();
    }

    @Override
    public void updateClassroomCategory(ClassroomCategoryUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateClassroomCategoryExists(updateReqVO.getId());
        // 更新
        ClassroomCategoryDO updateObj = ClassroomCategoryConvert.INSTANCE.convert(updateReqVO);
        classroomCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassroomCategory(String id) {
        // 校验存在
        this.validateClassroomCategoryExists(id);
        // 删除
        classroomCategoryMapper.deleteById(id);
    }

    private void validateClassroomCategoryExists(String id) {
        if (classroomCategoryMapper.selectById(id) == null) {
            throw exception(CLASSROOM_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public ClassroomCategoryDO getClassroomCategory(String id) {
        return classroomCategoryMapper.selectById(id);
    }

    @Override
    public List<ClassroomCategoryDO> getClassroomCategoryList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : classroomCategoryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ClassroomCategoryDO> getClassroomCategoryPage(ClassroomCategoryPageReqVO pageReqVO) {
        return classroomCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ClassroomCategoryDO> getClassroomCategoryList(ClassroomCategoryExportReqVO exportReqVO) {
        return classroomCategoryMapper.selectList(exportReqVO);
    }

    @Override
    public List<ClassroomCategoryDO> getClassroomCategoryListByStr(String name) {
         LambdaQueryWrapper<ClassroomCategoryDO> wrapper = new LambdaQueryWrapper<>();
         if (StringUtil.isNotBlank(name)){
             wrapper.like(ClassroomCategoryDO::getTypeName,name);
         }
        return classroomCategoryMapper.selectList(wrapper);
    }

}
